package com.hook3.pianke.view;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hook3.pianke.MainActivity;
import com.hook3.pianke.R;
import com.hook3.pianke.view.utils.CacheUtils;
import com.hook3.pianke.view.utils.DensityUtil;
import com.nineoldandroids.view.ViewHelper;
//import com.nineoldandroids.view.ViewHelper;

import java.util.ArrayList;

/**
 * 引导页
 *
 * @author 柴孟鑫
 */
public class GuideActivity extends Activity {
    private  ViewPager viewpager;
    private ArrayList<ImageView>imageViews;
    private int previousSelectPosition = 0;
    private  Button btn_start_main;
    private  LinearLayout ll_point_group;
    private int dipsize;
    private int smlsize;
    private int []ids= {R.mipmap.ic_guide_01, R.mipmap.ic_guide_02,
            R.mipmap.ic_guide_03, R.mipmap.ic_guide_04, R.mipmap.ic_guide_05};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        loadingVeiw();
    }

    private void loadingVeiw() {

        imageViews = new ArrayList<>();
        View view;
        for (int i = 0; i < ids.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(ids[i]);

            imageViews.add(imageView);
            view = new View(this);
            dipsize = DensityUtil.dip2px(this,10);
            smlsize = DensityUtil.dip2px(this,5);
            view.setBackgroundDrawable(getResources().getDrawable(R.drawable.image_normal_selector));
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(smlsize, smlsize);
            lp.leftMargin = dipsize;
            view.setLayoutParams(lp);
            view.setEnabled(false);
            ll_point_group.addView(view);


        }

        viewpager.setAdapter(new MyOnPagerAdapter());

        viewpager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewpager.setPageTransformer(false,new MyTransformer());
        ll_point_group.getChildAt(previousSelectPosition).setEnabled(true);

        viewpager.setCurrentItem(0);

        btn_start_main.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                CacheUtils.setBoolean(GuideActivity.this, SplashActivity.IS_START_MAIN, true);

                Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                startActivity(intent);

                //关于引导页面
                finish();

            }
        });
    }
    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            ll_point_group.getChildAt(previousSelectPosition).setEnabled(false);	// 把前一个点置为normal状态
            ll_point_group.getChildAt(position % imageViews.size()).setEnabled(true);		// 把当前选中的position对应的点置为enabled状态
            previousSelectPosition = position  % imageViews.size();
            if(position ==imageViews.size()-1){
                btn_start_main.setVisibility(View.VISIBLE);
            }else{
                btn_start_main.setVisibility(View.GONE);
            }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    class MyOnPagerAdapter extends  PagerAdapter{

        @Override
        public int getCount() {
            return imageViews.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(imageViews.get(position % imageViews.size()));
            return imageViews.get(position % imageViews.size());

        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view ==object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(imageViews.get(position % imageViews.size()));
        }
    }
    private void initView() {
        viewpager = (ViewPager) findViewById(R.id.viewpager);
        btn_start_main = (Button) findViewById(R.id.btn_start_main);
        ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);

    }

    public class MyTransformer implements ViewPager.PageTransformer {
        @Override
        public void transformPage(View page, float position) {
            if (position <= 0) {
                // 从右向左滑动为当前View
                // 设置旋转中心点；
                ViewHelper.setPivotX(page, page.getMeasuredWidth());
                ViewHelper.setPivotY(page, page.getMeasuredHeight() * 0.5f);
                // 只在Y轴做旋转操作
                ViewHelper.setRotationY(page, 90f * position);
            } else if (position <= 1) {
                // 从左向右滑动为当前View
                ViewHelper.setPivotX(page, 0);
                ViewHelper.setPivotY(page, page.getMeasuredHeight() * 0.5f);
                ViewHelper.setRotationY(page, 90f * position);
            }
        }

    }

}
